import classNames from 'classnames'
import { useEffect, useRef } from 'react'
import styles from './index.module.scss'
interface Props
  extends React.DetailedHTMLProps<React.TextareaHTMLAttributes<HTMLTextAreaElement>, HTMLTextAreaElement> {
  className?: string
  maxLength?: number
  value?: string
}
export default function Textarea({ className, maxLength = 10, value = '', ...rest }: Props) {
  const textAreaRef = useRef<HTMLTextAreaElement>(null)
  useEffect(() => {
    textAreaRef.current?.setSelectionRange(-1, -1)
  }, [])
  return (
    <div className={styles.root}>
      {/* 文本输入框 */}
      <textarea
        ref={textAreaRef}
        {...rest}
        value={value}
        maxLength={maxLength}
        className={classNames('textarea', className)}
      />
      {/* 当前字数/最大允许字数 */}
      <div className="count">
        {value?.length}/{maxLength}
      </div>
    </div>
  )
}
